package gstudio.views;

import gstudio.data.DataIO;
import gstudio.data.IData;
import gstudio.data.RuntimeDataManager;
import gstudio.images.MyImage;
import gstudio.ui.MyViewerContentProvider;
import gstudio.ui.MyViewerLabelProvider;

import java.util.List;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IMenuListener;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.jface.util.IPropertyChangeListener;
import org.eclipse.jface.util.PropertyChangeEvent;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.ListViewer;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.ui.actions.ActionGroup;
import org.eclipse.ui.part.ViewPart;

public class Artikel_Index extends ViewPart implements IPropertyChangeListener {
	private PropertyListenerManager plm = PropertyListenerManager.getInstance();

	private class MenuActions extends ActionGroup {

		private class CreateAction extends Action {
			public CreateAction() {
				setText("New");
				setImageDescriptor(new MyImage().get("new_wiz.gif"));
			}

			@Override
			public void run() {
				IData artikel = new IData();
				artikel.setValueOf("title", "_new_");

				runtimeDataManager.addArtikel(artikel);
				plm.sendChangeMeassage("newArtikel", artikel);
				listViewer.refresh();
			}
		}

		private class DelAction extends Action {
			public DelAction() {
				setText("Delete");
				setImageDescriptor(new MyImage().get("delete.gif"));
			}

			@Override
			public void run() {

				IStructuredSelection structuredSelection = (IStructuredSelection) listViewer
						.getSelection();
				Object obj = structuredSelection.getFirstElement();
				if (obj != null && obj instanceof IData) {
					IData idata = (IData) obj;
					input.remove(idata);
					System.out.println(input.size());
					listViewer.refresh();
				}
			}
		}

		private class SortAction extends Action {
			public SortAction() {
				setText("Sortieren");
				setImageDescriptor(new MyImage().get("ilow_obj.gif"));
			}

			@Override
			public void run() {
				System.out.println("Sortieren");
			}
		}

		@Override
		public void fillContextMenu(IMenuManager menu) {
			menu.add(new CreateAction());
			menu.add(new SortAction());
			menu.add(new Separator());
			menu.add(new DelAction());
		}
	}

	private ListViewer listViewer;
	private List<IData> input;

	private RuntimeDataManager runtimeDataManager = RuntimeDataManager
			.getInstance();

	private void createContextMenu() {

		MenuManager menuManager = new MenuManager();
		menuManager.setRemoveAllWhenShown(true);
		menuManager.addMenuListener(new IMenuListener() {
			public void menuAboutToShow(IMenuManager manager) {
				new MenuActions().fillContextMenu(manager);
			}
		});

		Control parent = listViewer.getControl();
		Menu menu = menuManager.createContextMenu(parent);
		parent.setMenu(menu);
		// getSite().setSelectionProvider(listViewer);
		// getSite().registerContextMenu(menuManager, listViewer);
	}

	@Override
	public void createPartControl(Composite parent) {

		plm.register(this);

		DataIO.getInstance().loadFile();

		listViewer = new ListViewer(parent, SWT.V_SCROLL);
		listViewer.setContentProvider(new MyViewerContentProvider());
		listViewer.setLabelProvider(new MyViewerLabelProvider());
		listViewer.addSelectionChangedListener(new ISelectionChangedListener() {
			@Override
			public void selectionChanged(SelectionChangedEvent event) {
				IStructuredSelection structuredSelection = (IStructuredSelection) event
						.getSelection();
				Object obj = structuredSelection.getFirstElement();
				if (obj != null) {
					runtimeDataManager.setCurrentData((IData) obj);
					plm.sendChangeMeassage("selectArtikel", obj);
				}
			}
		});

		createContextMenu();
		input = runtimeDataManager.getArtikelList();
		listViewer.setInput(input);
		selectFirst();
	}

	private void selectFirst() {
		if (input.size() > 0) {
			// 其他viewer还没有显示
			listViewer.getList().select(0);
			IData artikel = (IData) input.get(0);
			runtimeDataManager.setCurrentData(artikel);
		}
	}

	public ListViewer getListViewer() {
		return listViewer;
	}

	@Override
	public void propertyChange(PropertyChangeEvent event) {

		if (!event.getProperty().equalsIgnoreCase("updateArtikel")) {
			return;
		}

		Object obj = event.getNewValue();
		if (obj instanceof IData) {
			int i = listViewer.getList().getSelectionIndex();
			listViewer.setInput(runtimeDataManager.getArtikelList());
			if (i >= 0) {
				listViewer.getList().select(i);
			}
		}
	}

	@Override
	public void setFocus() {
	}

}
